//
//  IMSearchViewController.m
//  MyPhoneApp
//
//  Created by uuu9 on 16/10/9.
//  Copyright © 2016年 myApp. All rights reserved.
//

#import "IMSearchViewController.h"
#import "IMSearchResultViewController.h"
#import "IMSearchToolButton.h"
#import "PPiFlatSegmentedControl.h"
#import "IMSearchViewModel.h"
#import "SearchNavigationView.h"
#import "LightLine.h"
#import "SearchHelper.h"

@interface IMSearchViewController ()<searchDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) IMSearchViewModel *searchVM;

@property (nonatomic, strong) NSMutableArray *history;

@property (nonatomic, strong) NSMutableArray *displayArray;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) SearchNavigationView *titleView;

@end

@implementation IMSearchViewController


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    [self.titleView.textfield becomeFirstResponder];
}


-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (IMSearchViewModel *)searchVM{
    if (!_searchVM) {
        _searchVM = [IMSearchViewModel new];
    }
    return _searchVM;
}


- (SearchNavigationView *)titleView{
    if (!_titleView) {
        _titleView = [[SearchNavigationView alloc] initWithFrame:CGRectMake(0, 30, kScreenWidth, 44)];
        _titleView.delegate = self;
        _titleView.isSearchPop = YES;
    }
    return _titleView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.frame = CGRectMake(0, 74, kScreenWidth, kScreenHeight - 74);
        _tableView.separatorColor = UIColorFromRGB(kDetailLineColor);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.delegate = self;
        _tableView.dataSource = self;

        [_tableView setTableFooterView:[UIView new]];
        [_tableView setBackgroundColor:UIColorFromRGB(kBackGroundColor)];
        [_tableView setSeparatorColor:UIColorFromRGB(kDetailLineColor)];
    }
    return _tableView;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    self.view.backgroundColor = UIColorFromRGB(kBackGroundColor);
    NSMutableArray *array = [[[SearchHelper alloc] init] selectFromTable];
    [self.history addObjectsFromArray:array];
    [self.displayArray addObjectsFromArray:array];
    
    [self.view addSubview:self.titleView];
    [self.view addSubview:self.tableView];
    
}

- (NSMutableArray *)displayArray{
    if (!_displayArray) {
        _displayArray = [NSMutableArray array];
    }
    return _displayArray;
}



- (NSMutableArray *)history{
    if (!_history) {
        _history = [NSMutableArray array];
    }
    return _history;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // 取每个section对应的数组
    //NSArray *arr = [self.sectionDictionary objectForKey:self.keyArray[section]];
    return self.displayArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 55;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 55;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    static NSString *str = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    cell.backgroundColor = UIColorFromRGB(kBackGroundColor);
    cell.textLabel.text = self.displayArray[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:13];
    cell.textLabel.textColor = UIColorFromRGB(kDarkFontColor);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *toolsView = [[UIView alloc] initWithFrame:CGRectMake(0, -1, kScreenWidth, 55)];
    toolsView.backgroundColor = UIColorFromRGB(kBackGroundColor);
    LightLine *topLine = [[LightLine alloc] init];
    LightLine *bottomLine = [[LightLine alloc] init];
    [toolsView addSubview:topLine];
    [toolsView addSubview:bottomLine];
    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 1));
        make.top.offset(0);
    }];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 1));
        make.bottom.offset(0);
    }];
    IMSearchToolButton *history = [[IMSearchToolButton alloc] init];
    [history setImage:[UIImage imageNamed:@"search_history"] forState:UIControlStateNormal];
    [history setTitle:@"搜索记录" forState:UIControlStateNormal];
    [toolsView addSubview:history];
    [history mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(toolsView.mas_centerY);
        make.left.mas_equalTo(toolsView).offset(15);
        make.width.mas_equalTo(85);
    }];

    IMSearchToolButton *delete = [[IMSearchToolButton alloc] init];
    [delete setImage:[UIImage imageNamed:@"history_record"] forState:UIControlStateNormal];
    [delete setTitle:@"删除记录" forState:UIControlStateNormal];
    [toolsView addSubview:delete];
    [delete mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(toolsView.mas_centerY);
        make.right.mas_equalTo(toolsView.mas_right).offset(-15);
        make.width.mas_equalTo(85);

    }];
    [delete addTarget:self action:@selector(deleteSearchHistory) forControlEvents:UIControlEventTouchUpInside];

    return toolsView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = self.displayArray[indexPath.row];
    [self searchButtonClick:str];
}


- (void)deleteSearchHistory{
    [[[SearchHelper alloc] init] deleteFromTable];
    [self.displayArray removeAllObjects];
    [self.history removeAllObjects];
    [self.tableView reloadData];
}

-(void)backButtonClick{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)clearButtonClick{
    [self deleteSearchHistory];
}

- (void)searchButtonClick:(NSString *)searchStr{
    IMSearchResultViewController *resultController = [[IMSearchResultViewController alloc] init];
    resultController.searchString = searchStr;
    [self.navigationController pushViewController:resultController animated:YES];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.titleView.textfield resignFirstResponder];
}



@end
